﻿using Delphi.Mimir.Config;

namespace Delphi.Mimir.Data.Procedures {
    public class Etl {
        public static void EodSymbolsApi() {
            using(var conntext = new DelphiContext()) {
                conntext.Database.ExecuteSqlCommand("dbo.eod_symbols_api");
            }
        }

        public static void EodQuotesApi() {
            using (var conntext = new DelphiContext()) {
                conntext.Database.CommandTimeout = 1200;
                conntext.Database.ExecuteSqlCommand("dbo.eod_quotes_api");
            }
        }

        public static void EodCorporateActionsApi() {
            using (var conntext = new DelphiContext()) {
                conntext.Database.CommandTimeout = 1200;
                conntext.Database.ExecuteSqlCommand("dbo.eod_corporate_actions_api");
            }
        }

        public static void EodFundamentalsApi() {
            using (var conntext = new DelphiContext()) {
                conntext.Database.ExecuteSqlCommand("dbo.eod_fundamentals_api");
            }
        }

        public static void ClearTable(string tableName) {
            // Safety hatch: Do this only for raw and stage tables as truncate is very dangerous
            // For same reason not reflected as stored procedure
            string[] str = tableName.Split('.');
            if((str[0].ToUpper().Equals("RAW") || str[0].ToUpper().Equals("STG")) && str.Length == 2 ) {
                using (var conntext = new DelphiContext()) {

                    // Truncate table
                    string truncate = string.Format("TRUNCATE TABLE {0}", tableName);
                    conntext.Database.ExecuteSqlCommand(truncate);

                    // reset primary key
                    string reseed = string.Format("DBCC CHECKIDENT('{0}', RESEED, 1)", tableName);
                    conntext.Database.ExecuteSqlCommand(reseed);
                }
            }
        }
    }
}
